package com.cskaoyan.mall.search.service.impl;

import com.cskaoyan.mall.dto.AllItemResponse;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.search.api.ProductApi;
import com.cskaoyan.mall.search.converter.ProductConverter;
import com.cskaoyan.mall.search.entity.ItemDocument;
import com.cskaoyan.mall.search.repository.ProductRepository;
import com.cskaoyan.mall.search.service.InitDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Bowen Deng
 * @since 2022/07/16 11:03
 */
@Service
public class InitDataServiceImpl implements InitDataService {

    @Autowired
    ProductApi productApi;

    @Autowired
    ProductRepository productRepository;

    @Autowired
    ProductConverter productConverter;

    @Override
    public void initItems() {
        AllItemResponse allProductItem = productApi.getAllProductItem();
        List<ItemDto> allItems = allProductItem.getAllItems();
        List<ItemDocument> itemDocuments = productConverter.items2Documents(allItems);
        for (ItemDocument itemDocument : itemDocuments) {
            productRepository.save(itemDocument);
        }
    }
}
